﻿using UnityEngine;
using System.Collections;
using LitJson;
using NSNotifySystem;

public class NotifySystem : ISubModule
{
    static NotifySystem s_instance;

    static public NotifySystem Instance
    {
        get
        {
            if (s_instance == null)
            {
                s_instance = new NotifySystem();
            }

            return s_instance;
        }
    }

    NotifyText notify;

    /// <summary>
    /// 显示公告;
    /// </summary>
    public System.Action<string> Notify;

    /// <summary>
    /// 刷新公告;
    /// </summary>
    public System.Action<string> RefreshNotify;

    public void Init()
    {

    }

    public void Update()
    {
    }

    public void Purge()
    {
        s_instance = null;
    }
    public void Start()
    {
        notify = new NotifyText(handleOnUpdateText);
        QueryNewNotify(notify);
        EventDispatchers.Regist(EventKeys.EventKey_Notify, handleOnNotify);
    }

    void QueryNewNotify(IVersionStrategy version)
    {
        FileVersionMgr.Instance.CheckVersion_Version(version);
    }

    void handleOnNotify(Events.IEvent e)
    {
        if (string.IsNullOrEmpty(notify.text)) return;

        if (Notify != null)
            Notify(notify.text);
    }

    void handleOnUpdateText()
    {
        if (RefreshNotify != null)
        {
            RefreshNotify(notify.text);
        }

    }

    public void PrePurge()
    {
        EventDispatchers.Remove(EventKeys.EventKey_Notify, handleOnNotify);
    }
}

namespace NSNotifySystem
{
    class NotifyText : IVersionStrategy
    {
        const string Key_Notify_Version = "Notify";
        static string fileName { get { return Key_Notify_Version; } }
        string _text;
        System.Action OnUpdateText;

        public NotifyText(System.Action updateText)
        {
            OnUpdateText = updateText;
        }

        public string text
        {
            get
            {
                return _text;
            }
            protected set
            {
                if (_text != value)
                {
                    _text = value;
                    if (OnUpdateText != null)
                        OnUpdateText();
                }
            }
        }

        public string key { get { return Key_Notify_Version; } }

        public void ServerValid()
        {
            string url = FileDownloadMgr.DefaultPath + "/" + fileName + ".txt";
            FileDownloadMgr.Download(url, cb_onNotifyDownFinish);
        }

        public void LocalValid()
        {
            var txt = TextReadAndWrite.LoadFromPersistenPath(fileName);
            if (txt == null || txt.Length == 0) return;
            text = TextReadAndWrite.ConvertBytesToString(txt);
        }

        public void DefaultValid()
        {
            var txt = TextReadAndWrite.LoadFromResources(fileName);
            if (txt == null) return;
            text = txt.text;
        }

        public void ServerNull()
        {
            TextReadAndWrite.DeleteFromPersistenPath(fileName);
            FileVersionMgr.Instance.WriteLocalVersion_Version(this);
        }

        void cb_onNotifyDownFinish(DownloadResult ret, WWW www)
        {
            if (ret != DownloadResult.Success)
                return;

            text = www.text;
            TextReadAndWrite.WriteToPersistentPath(fileName, www.bytes);
            FileVersionMgr.Instance.WriteLocalVersion_Version(this);
        }
    }
}
